<template>
<div class="search-radios">
  <div class="search-head">
    <div class="search">
      <el-input placeholder="搜索预约项目" @change="filtervalue" v-model="searchvalue"  >
        <template #prepend >
          <el-icon><search /></el-icon>
          </template>
      </el-input>
    </div>
  </div>
  <div class="search-content">
    <div class="check-g" v-for="i in newList" :key="i.title">
      <div class="lable">{{i.title}}</div>
      <div class="content">
        <el-checkbox-group v-model="checkList">
          <el-checkbox @change="change"  v-for="item in i.children" :key="item" :label="item.id">{{item.name}}</el-checkbox>
        </el-checkbox-group>
      </div>
    </div>
  </div>
</div>
</template>

<script lang="ts">
import { Search } from '@element-plus/icons-vue'
import { defineComponent, PropType, reactive, ref } from '@vue/runtime-core';
import useRequet, { RespData } from "../../../hook/network"

export default defineComponent({
  components: {
    Search
  },
  props: {
    echoCheckList: {
      type: Array as PropType<number[]>
    }
  },
  setup(props, ctx) {
    
    const request = useRequet({ path: "" });

    let visitList = []  as VisitList[]

    type VisitList = {
      title: string,
      children: Array<ListItem>
    }
    
    type ListItem = {
      id: string,
      name: string,
    }

    request.sendRequest({
        address: 'sick/record/getCustomerSubscribeItems',
        params: {},
        success: (resp: RespData) => {
          if(resp.code !== 0) return 
          console.log(resp.data)
          newList.value = resp.data
          visitList = JSON.parse(JSON.stringify(resp.data))
          
        },
        fail: (resp: RespData) => {
          if(resp.code !== 0) return
          
        },
        method: "GET",
      })
    console.log(visitList)
    const checkList =  ref([] as number[])
    const searchvalue = ref('')
    const newList = ref([] as VisitList[])
    // newList.value = JSON.parse(JSON.stringify(visitList))
    console.log(newList.value)
    const filtervalue = (val: string) => {
      console.log(val)
      if(val) {
        newList.value.forEach((v:VisitList, idx: number) => {
        v.children = v.children.filter(f => {
            if(f.name.includes(val)) {
              console.log(f.id)
              return f.id
            } else {
              return
            }
            
          })
        })
      } else {
        console.log(visitList)
        console.log(newList.value)
        newList.value = JSON.parse(JSON.stringify(visitList))
        
        // newList.value = visitList
        
      }
      
    }

    console.log(newList.value)

    if(props.echoCheckList?.length) {
      checkList.value = props.echoCheckList
    }
    const change = () => {
      ctx.emit('checkListChange', checkList.value)
      console.log(checkList.value)
    }

    



    return {
      checkList,
      visitList,
      newList,

      searchvalue,
      filtervalue,
      change,

      request
    }
  }
})
</script>

<style lang="scss" scopde>
.search-radios {
  width: 100%;
  .search-head {
      display: flex;
      width: 100%;
    .search { 
      width: 100%;
      }
      .btns {
      width: 30px;
      line-height: 30px;
      height: 30px;
      background: #e6e3e3;
      text-align: center;
      margin-left: 20px;
      border-radius: $radius-1;
      box-sizing: border-box;
      cursor: pointer;
      }
  }
  .search-content {
    .check-g {
      display: flex;
      padding: 10px;
      padding-left: 0;
      // align-items: center;
      .lable {
        font-size: $fontSize-1;
        padding-right: 20px;
        width: 68px;
        color: $regularText;
      }
      .content {
        width: calc(100% - 60px);
      }
    }
  }
}
</style>